package com.woniu108.valueAdded.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu108.carService.commos.dto.ScoreDetailLogDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.valueAdded.client.PersonClient;
import com.woniu108.valueAdded.form.ScoreRecordsForm;
import com.woniu108.valueAdded.form.ScoreRecordsTimeForm;
import com.woniu108.valueAdded.model.ScoreDetailLog;
import com.woniu108.valueAdded.param.ScoreRecordsParam;
import com.woniu108.valueAdded.param.ScoreRecordsTimeParam;
import com.woniu108.valueAdded.service.ScoreDetailLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 积分明细表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/scoreDetailLog")
@Api(tags = "积分记录信息接口")
public class ScoreDetailLogController {

    @Resource
    private ScoreDetailLogService scoreDetailLogService;
    @Resource
    private RedisUtil redisUtil;
    public String getUserId(String token){
        String refreshToken=redisUtil.getString(token);
        String userId= JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        return userId;
    }
    /**
     * 记录用户增减积分历史
     * @param form 积分历史记录基础信息
     * @return result 是否正常访问接口结果
     */
    @PostMapping("/addOrReduceScoreRecords.api")
    @ApiOperation("记录用户增减积分历史")
    public Result addOrReduceScoreRecords(@RequestBody ScoreRecordsForm form){
        ScoreRecordsParam param= BeanUtil.toBean(form,ScoreRecordsParam.class);
        ScoreDetailLog log = new ScoreDetailLog();
        log.setUserId(param.getUserId());
        log.setScoreType(param.getScoreType());
        log.setModifyed(param.getModifyed());
        log.setCreateTime(LocalDateTime.now());
        log.setScore(param.getScore());
        scoreDetailLogService.save(log);
        return Result.getResult();
    }

    /**
     *
     * @param form 积分记录筛选信息
     * @param token
     * @return Page<ScoreDetailLogDto> 选择页码和筛选条件下的所有增减积分历史
     */
    @PostMapping("/userScoreRecords.c")
    @ApiOperation("查看用户增减积分历史")
    public Result<Page<ScoreDetailLogDto>> userScoreRecords(@RequestBody ScoreRecordsTimeForm form,@RequestHeader String token){
        ScoreRecordsTimeParam param=BeanUtil.toBean(form,ScoreRecordsTimeParam.class);
        String userId = this.getUserId(token);
        param.setUserId(userId);
        Page<ScoreDetailLogDto> page=scoreDetailLogService.userScoreRecords(param);
        return Result.newPageInstance(ScoreDetailLogDto.class).setData(page);
    }


}

